﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;//添加命名空间，可以使用OleDbConnection等对象
using System.Data;//添加命名空间，可以使用dataset对象
public partial class student_findstuden : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();//打开数据库连接
        if (Session["UserName"] == null && Session["UserNo"] == null)//没有经过正常连接途径打开页面
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登陆！');parent.location.herf='../login.aspx'</script>", false);
        if (!IsPostBack)//page.IsPostBack是一个标志，当前请求是否第一次打开，IsPostBack只有第一次打的时候是false,其他时候是true
        {
            string detID = Request.QueryString["deleteID"];//获取页面传递的ID号
            if (detID != null)
            {
                GridView1.PageIndex = int.Parse(Request.QueryString["pIndex"]);//获取页面传递的显示页索引
                delete(detID);//调用删除函数
            }
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string stuID = GridView1.DataKeys[e.NewEditIndex][0].ToString();
        //跳转到编辑学生页面，同时将参数ID传递到编辑页面（相当于重新打开网页）
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>location.herf='editStudentStart.aspx?stuID=" + stuID + "';</script>", false);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
            "if(confirm('确定删除该生的学籍?')){location.href='editStudent.aspx?deleteID=" + GridView1.DataKeys[e.RowIndex][1].ToString() + "&pIndex=" + GridView1.PageIndex.ToString() + "'}</Script>", false);

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;//获取所编辑的行的索引
        bind(Session["sDL"].ToString(), Session["sTB"].ToString());//调用数据绑定函数，将需要查询的字段和输入的查询内容作为参数传递

    }
    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        DropDownList1.SelectedIndex = 0;
        TextBox1.Text = "";
        Label1.Text = "";
        GridView1.DataSource = null;
        GridView1.DataBind();
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Session["sDL"] = "";
            Session["sTB"] = TextBox1.Text.Trim();
            if (DropDownList1.SelectedIndex == 0)
                Session["sDL"] = "sNo";
            else if (DropDownList1.SelectedIndex == 1)
                Session["sDL"] = "sName";
            else if (DropDownList1.SelectedIndex == 2)
                Session["sDL"] = "sCollege";
            else
                Session["sDL"] = "sClass";
            bind(Session["sDL"].ToString(), Session["sTB"].ToString());
        }

    }
    public void bind(string seField, string seValue)
    {
        string mysql = "select * from StudentTb   where   " + seField + "   like   '%" + seValue + "%'order by sNo";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();//OleDbDataAdapter表示要对access数据源执行sql语句存储过程
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = myDS;
            GridView1.DataKeyNames = new string[] { "sNo" };
            GridView1.DataBind();//绑定数据到显示列表
            TextBox1.Text = "";//清空提示信息
        }
        else
        {
            TextBox1.Text = "无法查到相关数据！";
            GridView1.DataSource = null;//装载空数据源
            GridView1.DataBind();//绑定数据到显示列表
        }
    }
    public void delete(string myNo)/*删除函数*/
    {
        string mystr = "select * from StudenTb   where cNo='" + myNo + "'";
        OleDbCommand mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();
        mystr = "delete * from ScoreTb  where cNo='" + myNo + "'";
        mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();
        mystr = "delete * from SelcourseTb  where cNo='" + myNo + "'";
        mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();

    }
}